package com.cc.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cc.entity.Dept;
import com.cc.service.DeptService;
import com.cc.service.impl.DeptServiceImpl;
import com.cc.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 33314
 * @version 1.0
 * @description: TODO
 * @date 2025/4/17 19:27
 */
@WebServlet("/deptServlet")
public class DeptServlet extends HttpServlet {
    private DeptService deptService = new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //json编码格式
        resp.setContentType("application/json;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        switch (method){
                case "showDept":
                showDept(req, resp);
                break;
        }
    }
    /**
     * @description: TODO
     * @author 展示部门
     * @date
     * @version 1.0
     */
    public void showDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取部门列表
        List<Dept> deptList = deptService.getDept();
        // 将部门列表包装在 data 字段中
        JSONObject result = new JSONObject();
        result.put("data", deptList);
        // 将 JSON 字符串写入响应
        resp.getWriter().write(result.toJSONString());
    }
}
